import path from "path";
import * as fs from 'fs';

/**
 * @en Registration method for the main process of Extension
 * @zh 为扩展的主进程的注册方法
 */
export const methods: { [key: string]: (...any: any) => any } = {
    /**
     * @en A method that can be triggered by message
     * @zh 通过 message 触发的方法
     */
    showLog() {
        console.log('Hello World');
    },
};

const exportDirName = 'pkg-export';
const exportDir = path.join(__dirname, '../../../assets', exportDirName);
const scriptDir = path.join(exportDir, 'fairygui');
const exportedDir = path.join(__dirname, '..', 'exported');

/**
 * @en Method Triggered on Extension Startup
 * @zh 扩展启动时触发的方法
 */
export function load() {
    // 如果不存在文件夹则创建
    if (!fs.existsSync(exportDir)) {
        fs.mkdirSync(exportDir);
    }
    if (!fs.existsSync(scriptDir)) {
        fs.mkdirSync(scriptDir);
    }
    // 将exported中的所有文件拷贝到exportDir中
    fs.readdirSync(path.join(exportedDir, "framework")).forEach(file => {
        if (file.endsWith('.ts')) {
            const targetFile = path.join(scriptDir, file);
            fs.copyFileSync(path.join(exportedDir, "framework", file), targetFile);
        }
    });
    fs.readdirSync(path.join(exportedDir, "custom")).forEach(file => {
        if (file.endsWith('.ts')) {
            const targetFile = path.join(scriptDir, file);
            if (fs.existsSync(targetFile))
                return;
            fs.copyFileSync(path.join(exportedDir, "custom", file), targetFile);
        }
    });
}

/**
 * @en Method triggered when uninstalling the extension
 * @zh 卸载扩展时触发的方法
 */
export function unload() { }
